package pure
{
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.system.*;
	
public class LoaderManager
{    
	public function LoaderManager()
	{
		initObj();
		initEvt();
	}

	private var _remainQueue				:Vector.<Object>;
	private var _currLoaded				:Object;
	private var _dataLoader				:URLLoader;
	private var _loader						:Loader;
	private var ____onEachFinish			:Function;
	private var ____onFinish				:Function;

	/**
	* 加入load队列
	* @title		标题
	* @url			地址
	* @key			文件名
	* @type		类型
	* @content
	*/
	public function addLoadData(title:String,url:String):void
	{
		var temp:Object = { };
		temp.title = title;
		temp.url = url;
		temp.key = url.slice(0, -4);
		temp.type = url.slice( -3);
		_remainQueue.push(temp);
	}

	/**
	* 开始加载
	* 后进先出顺序
	* @onEachFinish		单项加载完成回调
	* @onFinish			全部加载完成回调
	*/
	public function start(onEachFinish:Function=null,onFinish:Function=null):void
	{
		if (onEachFinish != null)____onEachFinish = onEachFinish;
		if (onFinish != null)____onFinish = onFinish;
		/* 全部加载结束 */
		if(_remainQueue.length==0)
		{
			 if (____onFinish != null)____onFinish();
			return;
		}
		/* 新的加载 */
		_currLoaded=(_remainQueue.shift());
		_dataLoader.load(new URLRequest(_currLoaded.url));
	}

	/* 停止 */
	public function stop():void
	{
		
	}

	/* 暂停 */
	public function pause():void
	{
		
	}

	 /* 回收 */
	public function dispose():void
	{
		_dataLoader.removeEventListener(Event.COMPLETE, ____onDataComplete);
		_dataLoader.removeEventListener(ProgressEvent.PROGRESS, ____onProgress);
		_dataLoader.removeEventListener(IOErrorEvent.IO_ERROR, ____onIOError);
		_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, ____onComplete);
	}




	/* ----------------------------    get/set    --------------------------- */




	 /* 当前加载 */
	public function get currLoaded():Object
	{
		return _currLoaded;
	}

	 /* 单例 */
	public static function get instance():LoaderManager
	{
	    if (_instance == null) 
	    {
		_instance = new LoaderManager();
	    }
	    return _instance;
	}
	private static var _instance:LoaderManager;




	/* ----------------------------    private    ---------------------------*/




	private function initObj():void
	{
		/* 加载列表 */
		_remainQueue = new Vector.<Object>();
		
		/* 数据加载器 */
		_dataLoader = new URLLoader();
		_dataLoader.dataFormat = URLLoaderDataFormat.BINARY;
		
		/* 显示对象加载器 */
		_loader = new Loader();
	}

	private function initEvt():void
	{
		_dataLoader.addEventListener(Event.COMPLETE, ____onDataComplete);
		_dataLoader.addEventListener(ProgressEvent.PROGRESS, ____onProgress);
		_dataLoader.addEventListener(IOErrorEvent.IO_ERROR, ____onIOError);
		_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, ____onComplete);
	}

	private function ____onDataComplete(e:Event):void
	{
		switch(_currLoaded.type)
		{
			case "swf":
				var context:LoaderContext = new LoaderContext();
				context.applicationDomain = ApplicationDomain.currentDomain;
				_loader.loadBytes(_dataLoader.data, context);
				break;
			default:
				_currLoaded.content=e.target.data;
				if (____onEachFinish != null)____onEachFinish();
				start();
				break;
		}
	}

	private function ____onIOError(e:IOErrorEvent):void
	{
		trace("加载错误:",e.text);
	}
	
	private function ____onProgress(e:ProgressEvent):void
	{

	}

	private function ____onComplete(e:Event):void
	{  
		_currLoaded.content=e.target.content;
		if (____onEachFinish != null)____onEachFinish();
		start();
	}
}
}